# -*- coding: utf8  -*-
import sys

'''
6tag：S，单个词；B，词首；E，词尾；M1/M2/M，词中
4tag：S，单个词；B，词首；E，词尾；M，词中
'''
home_dir = "./"
def splitWord(words):
    uni = words.decode('utf-8')
    li = list()    
    for u in uni:
        li.append(u.encode('utf-8'))
    return li    
 
#4 tag
#S/B/E/M
def get4Tag(li):
    length = len(li)
    ## print length
    if length   == 1:
        return ['S']
    elif length == 2:
        return ['B','E']
    elif length > 2:
        li = list()
        li.append('B')
    for i in range(0,length-2):
        li.append('M')
    li.append('E')
    return li

#6 tag
#S/B/E/M/M1/M2
def get6Tag(li):
    length = len(li)
    ## print length
    if length   == 1:
        return ['S']
    elif length == 2:
        return ['B','E']
    elif length == 3:
        return ['B','M','E']
    elif length == 4:
        return ['B','M1','M','E']
    elif length == 5:
        return ['B','M1','M2','M','E']
    elif length > 5:
        li = list()
        li.append('B')
        li.append('M1')
        li.append('M2')
    for i in range(0,length-4):
        li.append('M')
    li.append('E')
    return li
 
def saveDataFile(trainobj,testobj,isTest,word,handle,tag):
    if isTest:
        saveTrainFile(testobj,word,handle,tag)
    else:
        saveTrainFile(trainobj,word,handle,tag)
 
def saveTrainFile(fileobj,word,handle,tag): 
    if len(word) > 0:
        # wordli = splitWord(word) # 把词编程list，例“周戬”--> ['周', '戬']
        wordli = list(word)
        if tag == '4':
            tagli = get4Tag(wordli)
        if tag == '6':
            tagli = get6Tag(wordli)
        for i in range(0,len(wordli)):
            w = wordli[i]
            h = handle
            t = tagli[i]
            fileobj.write(w + '\t' + h + '\t' + t + '\n')
    else:
    ## print 'New line'
        fileobj.write('\n')
 
#B,M,M1,M2,M3,E,S
def convertTag(tag):    
    fileobj    = open( home_dir + 'people-daily.txt','r', encoding='utf-8')
    # fileobj    = open( home_dir + 'people-daily.txt','r')
    trainobj = open( home_dir + tag + '.train.data','w' )
    testobj  = open( home_dir + tag + '.test.data','w')
 
    arr = fileobj.readlines()
    i = 0
    for a in arr:
        i += 1
        a = a.strip('\r\n\t ') #去除首尾的空
        # print(a)
        if a=="":continue
        words = a.split(" ")
        # print(words)
        
        test = False
        if i % 10 == 0:
            test = True
        for word in words:
            # # print "---->", word
            word = word.strip('\t ')
            # print(word)
            if len(word) > 0:        
                i1 = word.find('[')
            if i1 >= 0:
                word = word[i1+1:]
            i2 = word.find(']')
            if i2 > 0:
                w = word[:i2]  # 去除[]
            word_hand = word.split('/')
            # # print "----",word
            w,h = word_hand # 左边是词，右边是标注
            # print(w, h)
            # print('-------------')
            
            if h == 'nr':    #ren min  奥马尔·哈桑·艾哈迈德·巴希尔/nr
                ## print 'NR',w
                if w.find('·') >= 0:
                    tmpArr = w.split('·')
                    for tmp in tmpArr:
                        saveDataFile(trainobj,testobj,test,tmp,h,tag)
                continue
            if h != 'm': # 不保存量词
                saveDataFile(trainobj,testobj,test,w,h,tag)
            
            if h == 'w':
                saveDataFile(trainobj,testobj,test,"","",tag) #split
 
    trainobj.flush()
    testobj.flush()
 

# sys.argv[0]表示代码本身文件路径
# Sys.argv[ ]其实就是一个列表，里边的项为用户输入的参数，关键就是要明白这参数是从程序外部输入的

if __name__ == '__main__':    
    if len(sys.argv) < 2:
        print('tag[6,4] convert raw data to train.data and tag.test.data')
    else:
        tag = sys.argv[1]
        convertTag(tag)
    # # print len(sys.argv)
